
var roadId;
$(function () {
	roadId = $.getUrlParam("id");
	//站点的下拉列表，显示本公司下的站点列表
	$.ajax({
	    url: "../station/getStationByComnum.htm",
	    cache: false,
	    type: "get",
	    dataType: "json",
	    async: false,
	    success: function (result) {
	        if (result.status == 0) {
	            var data = result.data;
	            addOptions(data, "#roadStart");	//初始化起点下拉框
	            addOptions(data, "#roadCross");	//初始化途径站点下拉框
	            addOptions(data, "#roadEnd");	//初始化终点下拉框
	        } 
	    }
	});
	
    //如果roadId不为为null则回显数据
    if(roadId != 'null'){
    	echoRoadInfo();
    }
 
    //点击保存的时候
    $("#save").click(function(){
    	//路线起点编号
    	var roadStart = $("#roadStart").val();
    	//路线终点编号
		var roadEnd = $("#roadEnd").val();
		var roadCross = '';	//路线途经站点编号的字符串
		var roadCrossName = '';	//路线途经站点城市的字符串
		var roadCrossArray = [];  //声明一个存多选下拉的数组
		//使用‘-’拼接站点编号的字符串
		$("#roadCross option:selected").each(function(){
			roadCross += $(this).val() + "-";
			roadCrossName += $(this).attr("data-city") + "-";
			roadCrossArray.push($(this).val());	//把多选下拉的值存入数组
		});
		if(roadStart == '' || roadEnd == ''|| roadCrossArray.length == 0){
    		parent.layer.msg('所有站点都不能为空！', {icon: 2});
    		return false;
    	}
		//判断多选是否超过10个站点
		if(roadCrossArray.length > 10){
			parent.layer.msg('最多选择10个途径站点！', {icon: 2});
    		return false;
		}
		//这个地方可以判断，如果length >0 ,上面已经判断。把最后一个'-'去掉
		roadCross = roadCross.substr(0, roadCross.length-1);
		roadCrossName = roadCrossName.substr(0, roadCrossName.length-1);
    	//判断增加或修改
		if (roadId == 'null') {
			addRoad(roadStart, roadEnd, roadCross, roadCrossName);
        } else {
        	modifyRoad(roadStart, roadEnd, roadCross, roadCrossName);
        }
    });
    
});

//初始化bootstrap-multiselect多选下拉框
function multiselect(obj){
	$(obj).multiselect({
		nonSelectedText:'--请选择--',
		nSelectedText:'个站点',
		allSelectedText:'全部',
		includeSelectAllOption: true,//全选  
        selectAllText: '全选',//全选的checkbox名称  
		buttonWidth: '600px',
		numberDisplayed: 1000,//当超过1000个标签的时候显示n个被选中  
		inheritClass: true//继承原来select的button的class
	});
}

//如果是编辑的话则回显数据
function echoRoadInfo(){
    $.ajax({
        url: "../road/echoRoadInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
	    data: {roadId: roadId},
        success: function (result) {
            if (result.status == 0) {
                var data = result.data;
                $("#editRoadForm").formatForm(data);	//两个单选下拉框回显数据
                //获取多选框需要显示的数据
                var stations = data.stations;
                //遍历数据，进行多选下拉框的回显
                for (var i = 0; i < stations.length; i++) {
                	//使用这种方法进行多option的选中
                	$("#roadCross").find("option[value = '"+stations[i].stationNum+"']").attr("selected",true);
				}
                $("#roadStart, #roadCross, #roadEnd").multiselect('refresh');	//必须要刷新，不然不显示回显数据
                return;
            }
            layer.msg(result.message, {icon: 2});
        }
    });
}


//关闭窗口
function closeForm() {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}


//更新路线信息
function modifyRoad(roadStart, roadEnd, roadCross, roadCrossName){
    $.ajax({
        url: "../road/modifyRoadInfo.htm",
        cache: false,
        type: "post",
        async: false,	//设置成false
        dataType: "json",
        data : {roadId:roadId, roadStart:roadStart, roadEnd:roadEnd, roadCross:roadCross, roadCrossName:roadCrossName},
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../road/getRoadInfoByPage.htm'});
            } else {
                parent.layer.msg(result.message, {icon: 2});
            }
            closeForm();
        }
    });
}


//增加路线
function addRoad(roadStart, roadEnd, roadCross, roadCrossName) {
    $.ajax({
        url: "../road/addToRoadInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        async: false,	//设置成false
        data: {roadStart:roadStart, roadEnd:roadEnd, roadCross:roadCross, roadCrossName:roadCrossName},
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../road/getRoadInfoByPage.htm'});
            } else {
                parent.layer.msg(result.message, {icon: 2});
            }
            closeForm();
        },error: function(jqXHR, textStatus, errorThrown){
        	console.log(textStatus);
        }
    });
    closeForm();
}


//站点下拉列表  追加到option中
function addOptions(data,selectId){
	for(var i=0;i<data.length;i++){
		$(selectId).append('<option value="' + data[i].stationNum + '" data-city="'+ data[i].stationCity +'">' + data[i].stationName + '</option>');
	}
	//初始化前必须要先把数据放入select中，不然显示不出数据
	multiselect(selectId);
}